package com.FunctionInterfaceDemo;

import java.util.Arrays;
import java.util.List;

/**
 *   Stream流式计算 +链式编程
 *
 * */
public class StreamDemo {
    //例 5个用户进行筛选
    // ID需为偶数、年龄大于23、用户名转为大写、用户名字母倒序、只输出一个用户。
    public static void main(String[] args) {
        User user = new User(1,"a",20);
        User user2 = new User(2,"b",24);
        User user3 = new User(3,"c",22);
        User user4 = new User(4,"d",22);
        User user5 = new User(5,"e",24);
        User user6 = new User(6,"f",24);

        //放进集合
        List<User> list = Arrays.asList(user,user2,user3,user4,user5,user6);

        //Stream流计算
        list.stream()
                .filter(u->{return u.getId()%2==0;})//ID需为偶数
                .filter(u->{return u.getAge()>23;})//年龄大于23
                .map(u->{return u.getName().toUpperCase();})//用户名转为大写 输出名字
                .sorted((uu1,uu2)->{return uu2.compareTo(uu1);})//用户名字母倒序
                .limit(1) //只输出一个
                .forEach(System.out::println);

    }
}
class User{
    private int id;
    private String name;
    private int age;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public User(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
